package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class StandardAction extends BaseAction<Standard> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private StandardService standardService;
	//保存收派信息
	@Action(value="standard_save",results={@Result(name="success",location="./pages/base/standard.html", type="redirect")})
	public String save() {
		standardService.save(model);
		return SUCCESS;
	}
	
	//采用ajax校验收派名称不能相同
	@Action(value="standard_validate",results={@Result(name="success",type="json")})
	public String validateName() {
		//获取表单输入的收派标准名称
		String name = model.getName();
		//根据名称查询
		List<Standard> list = standardService.findByName(name);
		if(list !=null && list.size() ==0) {
			ServletActionContext.getContext().getValueStack().push(true);//返回true表示名称没有相同的
		}else {
			ServletActionContext.getContext().getValueStack().push(false);//返回false表名名称相同不能使用该名称
		}
		return SUCCESS;
	}
	
	//分页显示收派标准信息
	@Action(value="standard_pageQuery",results={@Result(name="success", type="json")})
	public String pageQuery() {
		//调用业务层查询分页数据
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Standard> pagedata = standardService.findPageData(pageable);
		//返回客户端数据，需要total和rows
//		Map<String,Object> map = new HashMap<String,Object>();
//		map.put("total", pagedata.getTotalElements());
//		map.put("rows", pagedata.getContent());
//		//使用struts2-json-plugin将数据转换成就json格式
//		ServletActionContext.getContext().getValueStack().push(map);
		
		pushPageDataToValueStack(pagedata);
		return SUCCESS;
	}
	
	//属性驱动获取页面传过来的ids字符串
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}
	//根据id删除对应收派信息
	@Action(value="standard_delBatch",results={@Result(name="success",location="./pages/base/standard.html",type="redirect")})
	public String delBatch() {
		String[] idArr = ids.split(",");
		standardService.deleteStandard(idArr);
		return SUCCESS;
	}
	
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll() {
		//调用业务层查询所有收派标准的名称
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}
